<template>
  <div class="todo-list">
    <h2>Todo List</h2>
    <div class="input-container"> <!-- 添加一个容器来包含输入框和按钮 -->
      <input v-model="newTodoTitle" @keyup.enter="addTodo" placeholder="Add a new todo" />
      <button @click="addTodo">Add</button>
    </div>
    <ul>
      <li v-for="(todo, index) in todos" :key="index">
        <TodoItem
          :title="todo.title"
          :completed="todo.completed"
          @update:completed="updateTodoCompleted(index, $event)"
          @remove-todo="removeTodo(index)"
        />
      </li>
    </ul>
  </div>
</template>

<script>
import TodoItem from './TodoItem.vue';

export default {
  name: 'TodoList',
  components: {
    TodoItem
  },
  data() {
    return {
      todos: [],
      newTodoTitle: ''
    };
  },
  methods: {
    addTodo() {
      if (this.newTodoTitle.trim()) {
        this.todos.push({ title: this.newTodoTitle, completed: false });
        this.newTodoTitle = '';
      }
    },
    removeTodo(index) {
      this.todos.splice(index, 1);
    },
    updateTodoCompleted(index, completed) {
      this.todos[index].completed = completed;
    }
  }
}
</script>

<style scoped>
.todo-list {
  max-width: 400px;
  margin: 0 auto;
}
.input-container { /* 添加样式来使输入框和按钮在同一行 */
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}
input {
  flex: 1; /* 使输入框占据剩余空间 */
  padding: 8px;
  margin-right: 10px; /* 添加一些间距 */
}
button {
  padding: 8px 12px;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  margin-bottom: 10px;
}
</style>